import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/http/index.js'
import {userInfo} from '@/http/api.js'


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    username:'',
    isLogin: localStorage.getItem('token') ? true: false,
    token: localStorage.getItem('token') ? localStorage.getItem('token') : ''
  },
  getters:{
    formatusername(state){
      return state.username + '大爷';
    }
  },
  mutations: {
    setUsername(state, username){
      state.username = username;
    },
    setIsLogin(state, islogin){
      state.isLogin = islogin;
    },
    setToken(state, token){
      state.token = token;
    },

  },
  actions: {
    // 异步操作的mutations
    getUserinfo(context){
      // 发起异步获取用户信息的操作 
      // 拿到用户信息以后 通过 mutations 修改state的数据
      console.log('有人触发了 获取用户信息 action');
      console.log(this.axios);

      userInfo()
      .then(res=>{
        console.log(res);
        context.commit('setUsername', res.username)
      })
    }
  },
  modules: {
  }
})
